import cv2
import matplotlib.pyplot as plt
import numpy as np

# 读取图像
img1 = cv2.imread("flower.jpeg")

# 图像平移
h, w = img1.shape[:2]
M = np.float32([[1, 0, 100], [0, 1, 50]]) # 平移矩阵,y方向向下平移50，x方向向右平移100
dst = cv2.warpAffine(img1, M, (w, h))

# 图像显示
fig, axes = plt.subplots(1, 2, figsize=(10, 8), dpi=100)
axes[0].imshow(img1[:, :, ::-1])
axes[0].set_title("original")
axes[1].imshow(dst[:, :, ::-1])
axes[1].set_title("after translation")

plt.show()